package com.zkjl.protect_business.controller;

import com.zkjl.protect_business.common.*;
import com.zkjl.protect_business.entity.User;
import com.zkjl.protect_business.repository.UserRepository;
import com.zkjl.protect_business.service.FileService;
import com.zkjl.protect_business.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/file")
@Api(tags = {"文件上传"})
public class FileController {

    @Autowired
    FileService fileService;

    @Autowired
    JwtUtil jwtUtil;


    @Autowired
    UserRepository userRepository;

    /**
     * 上传图片
     * @param files  请求参数
     * @return
     */
    @ApiOperation(value = "上传图片", notes = "上传图片", httpMethod = "POST")
    @RequestMapping(value = "/upload/images",method = RequestMethod.POST,headers = "content-type=multipart/form-data")
    @ResponseBody
    public ApiResponse<List<String>> uploadImage(@RequestParam(value = "files") MultipartFile files[]){

        List<String> strings = fileService.uploadImage(files);

        return ApiResponse.ofSuccess(strings);

    }


    /**
     * 上传文件
     * @param files  请求参数
     * @return
     */
    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    @RequestMapping(value = "/upload/file",method = RequestMethod.POST,headers = "content-type=multipart/form-data")
    @ResponseBody
    @SystemLog(description = "上传文件")
    public ApiResponse<List<String>> uploadFile(@RequestParam(value = "files") MultipartFile files[]){

        List<String> strings = fileService.uploadFile(files);

        return ApiResponse.ofSuccess(strings);

    }




    @ApiOperation(value = "下载文件", notes = "下载文件", httpMethod = "GET")
    @GetMapping(value = "{fileId}")
    @SystemLog(description = "下载文件")
    public void download(HttpServletResponse response,@PathVariable String fileId,String token) throws IOException {


        if (StringUtils.isBlank(token)) {
            throw new BaseException(Status.BAD_REQUEST.getCode(), "token异常");
        }
        String subToken = token.substring(7);

        String username = jwtUtil.getUsernameFromJWT(subToken);
        User user = userRepository.findByUserName(username);
        //登录成功
        UserThreadLocal.set(user);//将user对象放置在本地线程中，方便controller和service获取

        fileService.download(response, fileId);
    }


    @ApiOperation(value = "文件预览", notes = "目前只支持Word excel", httpMethod = "GET")
    @GetMapping(value = "{fileId}/preview")
    public ApiResponse preview(@PathVariable String fileId){

        String preview = fileService.preview(fileId);
        return ApiResponse.ofSuccess(preview);

    }




}
